package com.wind.taobao.api.trade.item.convert
{
	import com.wind.taobao.api.trade.item.model.ItemCategory;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * 商品查询分类结果转换器
	 * @author fxy
	 *
	 */
	public class ItemCategoryConvert
	{
		/**
		 * 转换ItemCategory集合
		 */
		public static function convertItemCategorys(value:Object, type:String="json"):ArrayCollection
		{
			var itemCategorys:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							itemCategorys.addItem(convertItemCategory((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return itemCategorys;
		}

		/**
		 * 转换ItemCategory
		 */
		public static function convertItemCategory(value:Object, type:String="json"):ItemCategory
		{
			var itemCategory:ItemCategory=new ItemCategory();
			if (value == null)
				return itemCategory;
			if (type == TaobaoConstants.XML)
			{
				itemCategory=convertXMLToItemCategory(XML(value), itemCategory);
			}
			else
			{
				itemCategory=convertJSONToItemCategory(value, itemCategory);
			}
			return itemCategory;
		}

		/**
		 * JSON转换为ItemCategory
		 */
		private static function convertJSONToItemCategory(json:Object, itemCategory:ItemCategory):ItemCategory
		{
			if (json.hasOwnProperty(TaobaoConstants.CATEGORY_ID))
			{
				itemCategory.id=json[TaobaoConstants.CATEGORY_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.COUNT))
			{
				itemCategory.count=json[TaobaoConstants.COUNT];
			}
			return itemCategory;
		}

		/**
		 * XML转换为ItemCategory
		 */
		private static function convertXMLToItemCategory(value:XML, itemCategory:ItemCategory):ItemCategory
		{
			return itemCategory;
		}


	}
}